//
//  NYMoreReplyViewController.m
//  Nooryes
//
//  Created by uuu9 on 2018/9/11.
//  Copyright © 2018年 UUU9. All rights reserved.
//

#import "NYMoreReplyViewController.h"
#import "NYPostReplyTableViewCell.h"
#import "NYCommentListModel.h"
#import "LineView.h"

@interface NYMoreReplyViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *commentNum;

@end

@implementation NYMoreReplyViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
    self.commentNum.text = STR_FROM_INT(self.model.replaylist.count);
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    } else {
        return self.model.replaylist.count;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        CommentListSubModel *model = self.model;
        return model.contentHeight + 75;
    } else {
        CommentListSubModel *model = [self.model.replaylist objectAtIndex:indexPath.row];
        return model.contentHeight + 75;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CommentListSubModel *submodel;
    if (indexPath.section == 0) {
        submodel = self.model;
    } else {
        submodel = [self.model.replaylist objectAtIndex:indexPath.row];
    }
    NYPostReplyTableViewCell *cell = [NYPostReplyTableViewCell cellWithTableView:tableView];
    cell.model = submodel;
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    headerView.backgroundColor = [UIColor whiteColor];
    UILabel *jclabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 200, 40)];
    jclabel.font = [UIFont boldSystemFontOfSize:15];
    jclabel.textColor = UIColorFromRGB(0x333333);
    if (section == 0) {
        jclabel.text = [NSString stringWithFormat:@"查看回帖%li条",self.model.replaylist.count];
    } else {
        jclabel.text = @"全部跟帖";
    }
    [headerView addSubview:jclabel];
    LineView *line = [[LineView alloc] initWithFrame:CGRectMake(0, 39.5, kScreenWidth, 0.5) andColor:UIColorFromRGB(0xededed)];
    [headerView addSubview:line];
    
    return headerView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CommentListSubModel *submodel;
    if (indexPath.section == 0) {
        submodel = self.model;
    } else {
        submodel = [self.model.replaylist objectAtIndex:indexPath.row];
    }
    _commentBlock(submodel.commentid);
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    [textField resignFirstResponder];
    _commentBlock(_model.commentid);
    return NO;
}

- (IBAction)onDismissView:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
